package com.cy.server.controller;


import com.cy.server.dto.RoleDto;
import com.cy.server.pojo.Admin;
import com.cy.server.pojo.RespBean;
import com.cy.server.pojo.Role;
import com.cy.server.service.IAdminService;
import com.cy.server.service.IRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiShaoZhuo
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;
    @Autowired
    IRoleService roleService;

    @ApiOperation("获取所有操作员")
    @GetMapping("/list")
    public List<Admin> getAllAdmins(String keywords){
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation("更新操作员")
    @PostMapping("/update")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if (adminService.updateById(admin)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation("删除操作员")
    @DeleteMapping("/delete/{id}")
    public RespBean deleteAdmin(@PathVariable Integer id){
        if (adminService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation("角色信息分页")
    @PostMapping("/role")
    public RespBean selectAllRole(@RequestBody RoleDto roleDto){
        PageInfo<Role> pageInfo = roleService.selectRole(roleDto);
        return RespBean.success(pageInfo);
    }
    @ApiOperation("更新操作员角色")
    @PostMapping("/updateRole")
    public RespBean updateAdminRole(Integer adminId, Integer[] rids){
        return adminService.updateAdminRole(adminId,rids);
    }
}
